package com.example.day14;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    public void onCreate() {
        super.onCreate();
        Log.e("service####onCreate", "onCreate");
    }

    //服务执行的操作
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("service###onStartCom", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    //销毁服务时调用
    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
        Log.e("service#####onDestroy", "销毁回调");
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.e("####onBind", "服务绑定回调");
        Log.e("####接收数据", intent.getStringExtra("info"));
        return new MyBinder();
    }

    @Override
    public void onRebind(Intent intent) {
        Log.e("####onRebind", "服务再绑定回调");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {

        Log.e("####onUnbind", "服务解绑回调");
        return super.onUnbind(intent);
    }

    class MyBinder extends Binder {
        public MyService getService(){
            return MyService.this;
        }

        public void startDownload(){

            Log.e("#######","startDownload");
        }

        public int getProgress(){

            Log.e("########","getProgress");
            return method2();
        }
    }

    //服务中提供的方法
    public void method1(){
        Toast.makeText(this, "service中的方法1", Toast.LENGTH_SHORT).show();
    }
    //服务中提供的方法
    public int method2(){
        Toast.makeText(this, "service中的方法2", Toast.LENGTH_SHORT).show();
        return 222;
    }


}
